import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import("../views/login/Login");
const Home = () => import("../views/Home");
const Users = () => import("../views/users/Users");
const Welcome = () => import("../views/welcome/Welcome");
const Roles = () => import("../views/rights/Roles");
const Rights = () => import("../views/rights/Rights");
const GoodsList = () => import("../views/goods/GoodsList");
const Reports = () => import("../views/reports/Reports");
const Orders = () => import("../views/orders/Orders");
const Cate = () => import("../views/goods/Cate");
const Params = () => import("../views/goods/Params");
const Add = () => import("../views/goods/Add");
const Edit =  () => import("../views/goods/Edit");
Vue.use(VueRouter);
const routes = [
  {
    path: '',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/users',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path:'/goods',
        component:GoodsList
      },
      {
        path:'/reports',
        component:Reports
      },
      {
        path:'/orders',
        component:Orders
      },
      {
        path:'/categories',
        component:Cate
      },
      {
        path:'/params',
        component:Params
      },
      {
        path:'/goods/add',
        component:Add
      },
      {
        path:'/goods/edit',
        component:Edit
      }
    ]
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

//守卫路由
router.beforeEach(((to, from, next) => {
  if (to.path === '/login') return next();
  let token = window.sessionStorage.getItem('token');
  if (token) return next();
  return next('/login');
}));


export default router
